#include "stdafx.h"
#include "Hand.h"
#include "HandInfo.h"
#include "Player.h"
#include "CcpfHandInfoBuilder.h"
#include <algorithm>

// calls a preflop raise when they haven't yet got any money in the pot, including blinds

bool HasMoneyInPot(string playerName, list<string> playersWithMoneyInPot)
{
	return !(find(playersWithMoneyInPot.begin(), playersWithMoneyInPot.end(), playerName)
					== playersWithMoneyInPot.end());
}

void CcpfHandInfoBuilder::Fill(HandInfo &handInfo, const Hand &hand)
{
	list<Action> actions = hand.Actions();
	list<Action>::const_iterator actionIt;
	list<string> playersWithMoneyInPot;
	bool isRaisedPot = false;
	
	for (actionIt = actions.begin(); actionIt != actions.end(); actionIt++)
	{
		if (actionIt->StreetPos() != PREFLOP)
			return;

		ActionType actionType = actionIt->ActionType();
		string playerName = actionIt->PlayerName();

		if (actionType == RAISE)
		{
			if (isRaisedPot && !HasMoneyInPot(playerName, playersWithMoneyInPot))
			{				
				handInfo.PlayerInfo(playerName).CcpfOp(true);
			}
			isRaisedPot = true;
			playersWithMoneyInPot.push_back(playerName);
		}
		else if (actionType == CALL)
		{
			if (isRaisedPot && !HasMoneyInPot(playerName, playersWithMoneyInPot))
			{
				handInfo.PlayerInfo(playerName).Ccpf(true);
				handInfo.PlayerInfo(playerName).CcpfOp(true);
			}
			playersWithMoneyInPot.push_back(playerName);
		}
		else if (actionType == FOLD)
		{
			if (isRaisedPot && !HasMoneyInPot(playerName, playersWithMoneyInPot))
			{
				handInfo.PlayerInfo(playerName).CcpfOp(true);
			}
		}
		else if (actionType == POST)		
		{			
			playersWithMoneyInPot.push_back(playerName);
		}
	}
}